package tests;

public class SingleList <T>{
	public static void main(String[] args) {
		SingleList<Integer> sl1 = new SingleList<Integer>();
		sl1.add(new Integer(5));
		sl1.add(new Integer(6));
		sl1.delete(0);
		sl1.print();
	}
	
	Node m_Head;
	
	SingleList(){
		m_Head=null;
	}
	
	class Node{
		Node m_next;
		T m_data;
		Node(T data, Node next){
			m_data=data;
			m_next=next;
		}
	}
	
	public void add(T data){
			m_Head=new Node(data,m_Head);
	}
	
	public T get(int index){
		Node temp=m_Head;
		int counter =0;
		while(counter<index&&temp!=null){
			++counter;
			temp=temp.m_next;
		}
		return temp.m_data;
	}
	
	public void set(int index, T data){
		Node temp = m_Head;
		int counter=0;
		while(counter<index&&temp!=null){
			++counter;
			temp=temp.m_next;
		}
		temp.m_data=data;
	}
	
	public void delete(int index){
		Node temp1=m_Head;
		Node temp2=m_Head.m_next;
		int counter=0;
		
		while(temp1!=null&&counter<index-1){
			temp1=temp1.m_next;
			temp2=temp2.m_next;
			++counter;
		}
		
		if(temp1==m_Head){
			m_Head=m_Head.m_next;
		}
		
		else
			temp1.m_next=temp2.m_next;
	}

	public void print(){
		Node temp =m_Head;
		while(temp!=null){
			System.out.println(temp.m_data);
			temp=temp.m_next;
		}
	}
}
